/******************************************************************
 *  MRRM's testbench - Arithmetic_instruction                     *
 *                                                                *
 *  This file is part of the MRRM project                         *
 *  <http://mrrm.googlecode.com/>                                 *
 *                                                                *
 *  Author(s):                                                    *
 *    -  Zhou Jing                                                *
 *                                                                *
 ******************************************************************
 *                                                                *
 *  Copyright (C) 2010 AUTHORS                                    *
 *                                                                *
 *  This source file may be used and distributed without          *
 *  restriction provided that this copyright statement is not     *
 *  removed from the file and that any derivative work contains   *
 *  the original copyright notice and the associated disclaimer.  *
 *                                                                *
 *  MRRM is free software: you can redistribute it and/or modify  *
 *  it under the terms of the GNU General Public License as       *
 *  published by the Free Software Foundation, either version 3   *
 *  of the License, or (at your option) any later version.        *
 *                                                                *
 *  MRRM is distributed in the hope that it will be useful, but   *
 *  WITHOUT ANY WARRANTY; without even the implied warranty of    *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  *
 *  GNU General Public License for more details.                  *
 *                                                                *
 *  You should have received a copy of the GNU General Public     *
 *  License along with MRRM. If not, see                          *
 *  <http://www.gnu.org/licenses/>.                               *
 *                                                                *
 ******************************************************************/
module test_ari_inst;

// Clock and reset
reg clk;
reg rst;

// Memory
reg [31:0] mem [0:255];
reg [7:0] temp;
reg [7:0] temp2;
wire [31:0] imemaddr;
reg [31:0] imemdata;
wire [31:0] dmemaddr;
wire [31:0] dmemdataw;
reg [31:0] dmemdatar;
wire dmemwr;
wire dmemrd;
wire dmembyte;

// CPU instance
mrrm_cpu cpu(.clk(clk), .rst(rst),
	.imemaddr(imemaddr), .imemdata(imemdata),
	.dmemwr(dmemwr), .dmemrd(dmemrd), .dmembyte(dmembyte),
	.dmemaddr(dmemaddr), .dmemdataw(dmemdataw), .dmemdatar(dmemdatar));

// Memory control
always @(imemaddr)
	imemdata = mem[imemaddr/4];

always @(posedge clk)
	if(dmemwr == 1'b1) begin
		if(dmembyte == 1'b1)
			case(dmemaddr%4)
				0: begin
				temp = mem[dmemaddr/4];
				temp[0*8+7:0*8] = dmemdataw[7:0];
				mem[dmemaddr/4] = temp;
				end
				1: begin
				temp = mem[dmemaddr/4];
				temp[1*8+7:1*8] = dmemdataw[7:0];
				mem[dmemaddr/4] = temp;
				end
				2: begin
				temp = mem[dmemaddr/4];
				temp[2*8+7:2*8] = dmemdataw[7:0];
				mem[dmemaddr/4] = temp;
				end
				3: begin
				temp = mem[dmemaddr/4];
				temp[3*8+7:3*8] = dmemdataw[7:0];
				mem[dmemaddr/4] = temp;
				end
			endcase
		else
			mem[dmemaddr/4] = dmemdataw;
	end

always @(dmemaddr or dmemrd or dmembyte)
	if(dmemrd == 1'b1) begin
		if(dmembyte == 1'b1)
			case(dmemaddr%4)
				0: begin
				temp2 = mem[dmemaddr/4];
				dmemdatar = {24'b0, temp2[0*8+7:0*8]};
				end
				1: begin
				temp2 = mem[dmemaddr/4];
				dmemdatar = {24'b0, temp2[1*8+7:1*8]};
				end
				2: begin
				temp2 = mem[dmemaddr/4];
				dmemdatar = {24'b0, temp2[2*8+7:2*8]};
				end
				3: begin
				temp2 = mem[dmemaddr/4];
				dmemdatar = {24'b0, temp2[3*8+7:3*8]};
				end
			endcase
		else
			dmemdatar = mem[dmemaddr/4];
	end

// Initialize
integer file;

initial
	file = $fopen("test_ari_inst-out.txt");

initial
	$readmemh("test_ari_inst-in.txt", mem);

// monitor has not been changed
initial
$fmonitor(file, "[%04d] %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d ", 
	 $time, mem[74], mem[75], mem[76], mem[77], mem[78], mem[79], mem[80], mem[81], mem[82], mem[83], mem[84], mem[85], mem[86], mem[87], mem[88], mem[89],);
initial
	begin
		rst = 1;
		clk = 1;
		#1 rst = 0;
		#4000 $stop;
	end

// Clock
always
	#5 clk = ~clk;

endmodule


